/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Facade;

import entity.Customer;
import java.util.logging.Logger;
import javax.annotation.security.RolesAllowed;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.persistence.Query;
//
/**
 *
 * @author Vojtisek
 */
@Stateless
public class CustomerFacade implements CustomerFacadeLocal {

    @PersistenceContext
    private EntityManager em;
//    private static final Logger logger = Logger.getLogger("CustomerFacade");
    @Resource
    private SessionContext ctx;

    @RolesAllowed("customer")
    public Customer getCustomer() throws Exception {
        String login = ctx.getCallerPrincipal().getName();
        Query query = em.createQuery("select c from Customer c where c.login=:login");
        query.setParameter("login", login);
        Customer cust = (Customer) query.getSingleResult();
        return cust;
    }
}
